package health.lm.com.component_base.base.mvp.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.LifecycleTransformer;

import butterknife.Unbinder;
import health.lm.com.component_base.R;
import health.lm.com.component_base.base.event.BusProvider;
import health.lm.com.component_base.base.mvp.kit.KnifeKit;
import health.lm.com.component_base.base.mvp.contract.VDelegateBase;
import health.lm.com.component_base.base.mvp.inner.IPresent;
import health.lm.com.component_base.base.mvp.inner.IView;
import health.lm.com.component_base.base.mvp.inner.VDelegate;

/**
 * Created by wanglei on 2017/1/26.
 */

public abstract class XLazyFragment<P extends IPresent> extends LazyFragment implements IView<P> {

    private VDelegate vDelegate;
    private P p;
    public ImmersionBar mImmersionBar;
    private RxPermissions rxPermissions;
    private Unbinder unbinder;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        rxPermissions = new RxPermissions(context);
        initImmersionBar();
        getP();

        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
            bindUI(getRealRootView());
        }
        if (useEventBus()) {
            BusProvider.getBus().register(this);
        }
        bindEvent();
        initData(savedInstanceState);
    }

    @Override
    public void bindUI(View rootView) {
        unbinder = KnifeKit.bind(this, rootView);
    }

    @Override
    public void bindEvent() {

    }

    public VDelegate getvDelegate() {
        if (vDelegate == null) {
            vDelegate = VDelegateBase.create(context);
        }
        return vDelegate;
    }

    protected P getP() {
        if (p == null) {
            p = newP();
        }
        if (p != null) {
            if (!p.hasV()) {
                p.attachV(this);
            }
        }
        return p;
    }

    @Override
    protected void onDestoryLazy() {
        super.onDestoryLazy();
        if (useEventBus()) {
            BusProvider.getBus().unregister(this);
        }
        if (getP() != null) {
            getP().detachV();
        }
        getvDelegate().destory();
//        KnifeKit.unbind(unbinder);
        p = null;
        vDelegate = null;
    }


    @Override
    public int getOptionsMenuId() {
        return 0;
    }


    @Override
    public boolean useEventBus() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }

    private void initImmersionBar() {
        if (mImmersionBar == null) {
            mImmersionBar = ImmersionBar.with(this);
            mImmersionBar.statusBarColor(R.color.colorPrimary).statusBarDarkFont(true).init();
        }
    }

    /**
     * 打开一个Activity 默认 不关闭当前activity
     */
    public void gotoActivity(Class<?> clz) {
        gotoActivity(clz, false, null);
    }

    public void gotoActivity(Class<?> clz, boolean isCloseCurrentActivity) {
        gotoActivity(clz, isCloseCurrentActivity, null);
    }

    public void gotoActivity(Class<?> clz, boolean isCloseCurrentActivity, Bundle ex) {
        Intent intent = new Intent(context, clz);
        if (ex != null) {
            intent.putExtras(ex);
        }
        startActivity(intent);
        if (isCloseCurrentActivity) {
            ((Activity) context).finish();
        }
    }


    /**
     * 无参数传递跳转到Activity
     * 无跳转动画
     */
    public void gotoActivity(String path) {
        ARouter.getInstance().build(path).navigation();
    }


    /**
     * 带参数传递跳转到Activity
     * 无跳转动画
     */
    public Postcard withValueActivity(String path) {
        Postcard aRouter = ARouter.getInstance().build(path);
        return aRouter;
    }
}
